package com.ysd.twocrm.mapper;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.ysd.twocrm.entity.Users;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

public interface UsersMapper extends BaseMapper<Users> {

    //用于班级查询

    //查询辅导员或者教员的身份或者咨询师或者游客
@Select(value = "select * from users where userId in(select userId from userroles where roleId in (select roleId from roles where roleName=#{roleName}))")
    List<Users> findAllUsers(String roleName);
//查询自己对应的咨询师
    @Select(value = "select * from users where userid =(select iscounselor from users where userid=#{userId})")
    Users findAllIsCounselor(Integer userId);
//在班级表格内查询辅导员和教员的姓名
    @Select(value = "select * from users where userId=#{userId}")
    Users getAllRoleNameById(Integer userId);

//修改锁定按钮 :根据id来修改当前是否锁定
    @Update(value = "update users set userIsLockOut=#{userIsLockOut},userLockTime=now() where userId=#{userId}")
    Integer exituserIsLockOut(Integer userId,Integer userIsLockOut);
    //根据id来修改锁定时间 $:不带引号 #：带引号
    @Update(value = "update users set userLockTime=${param} where userId=#{userId}")
    Integer exituserIsLockOutTime(Integer userId,String param);
    //根据用户名和密码来查询
    @Select(value = "select * from users where userLoginName=#{userLoginName}")
    Users selectLogin(String userLoginName);
    //根据用户名来修改密码错误次数，每次输错一次则就次数加1
    @Update(value = "update users set userpsdWrongSum=userpsdWrongSum+1 where userLoginName=#{userLoginName}")
    Integer exitusersPwdByloginName(String userLoginName);
    //根据用户id来修改密码错误次数，每次解锁就将密码错误次数清空为0
    @Update(value = "update users set userpsdWrongSum=0 where userId=#{userId}")
    Integer exitusersPwdIdById(Integer userId);
    //修改最后登录时间
    @Update(value = "update users set userLastLoginTime=now() where userId=#{userId}")
    Integer exitUsersLastLoginTime(Integer userId);
    //查看没有咨询师角色的用户（要求用户身份是游客）
@Select(value = "select * from users where userId in(select userId from userroles where roleId in (select roleId from roles where roleName='游客')) and IsCounselor=0")
    List<Users> findCounselor();
    //验证登录名
    @Select(value = "select * from users where userLoginName=#{userLoginName}")
    Users findUserLoginName(String userLoginName);
    //验证手机号
    @Select(value = "select * from users where userProtectMtel=#{userProtectMtel}")
    Users findUserPhone(String userProtectMtel);
    //根据手机号来修改用户密码
    @Update(value = "update users set userPassword=#{userPassword}   where userProtectMtel=#{userProtectMtel}")
    Integer exitUsersPwdByPhone(String userPassword,String userProtectMtel);
    //修改字段，是否用户自动分配咨询师
    @Update(value = "update users set exit1=#{exit1}")
    Integer exitUserExit1(Integer exit1);
   //查询第一条用户数据
    @Select(value = "select * from users order by userid limit 0,1")
    Users findUsersExit1();
    //查询权重值最高的咨询师
    @Select(value = "select * from users where userId in(select userId from userroles where roleId in (select roleId from roles where roleName=#{rolename}) order by userrolesweight desc) limit 0,1")
    Users findWeightRoles(String rolename);
    //查询全部咨询师
    @Select(value = "select * from users where userId in(select userId from userroles where roleId in (select roleId from roles where roleName=#{roleName})) and username like concat('%',#{userName},'%')")
    <E extends IPage<Users>> E selectconsoler(E page, String userName,String roleName);
    //给当前用户换咨询师
@Update(value = "update users set IsCounselor=#{IsCounselor} where userId=#{userId}")
    Integer exitUsersConsoler(Integer userId,Integer IsCounselor);
//根据咨询师查询用户

    //修改用户的角色为管理员或者学生或者员工或者游客
    @Update(value = "update users set isUserRoleId=#{isUserRoleId} where userId=#{userId}")
    Integer exitUsersRolesId(Integer userId,Integer isUserRoleId);

}
